package com.sbl.bajw.pc.action;

import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.ExcelUtil;
import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.SessionUnit;
import com.sbl.bajw.pc.model.SspoiModel;
import com.sbl.bajw.pc.service.SspoiModelService;
import com.sbl.bajw.util.JsonUtil;
import com.sbl.bajw.util.LogUtil;

@Controller
@RequestMapping("/SspoiModel")
public class SspoiModelAction {
	
	@Resource
	private SspoiModelService service;
	
	static Logger log;
	
	static {
		log = LogUtil.getLogger("SspoiModel");
	}
	
	@RequestMapping(value = "/getSspoiGrid")
	public @ResponseBody String getSspoiGrid(HttpServletRequest request, HttpServletResponse response){
		SspoiModel model = RequestUnit.getModel(SspoiModel.class);
		HashMap<String, Object> parm = new HashMap<String, Object>();
		if(model.getAddr() != null && !model.getAddr().equals("")){
			parm.put("addr", "%" + model.getAddr() + "%");
		}
		if(model.getPoiname() != null && !model.getPoiname().equals("")){
			parm.put("mc", "%" + model.getPoiname() + "%");
		}
		if(model.getTypeid() != null && !model.getTypeid().equals("")){
			parm.put("type", model.getTypeid());
		}
		if(model.getPcsid() != null && !model.getPcsid().equals("")){
			parm.put("pcs", model.getPcsid());
		}
		if(model.getJqid() != null && !model.getJqid().equals("")){
			parm.put("jq", model.getJqid());
		}
		if(model.getIsmarked() != null && !model.getIsmarked().equals("")){
			parm.put("ismark", model.getIsmarked());
		}
		log.info("==>addr="+model.getAddr()+",poiname="+model.getPoiname()+",typeid="+model.getTypeid()+",pscid="+model.getPcsid()+",jqid="+model.getJqid()+",ismarked="+model.getIsmarked());
		String json = service.getSspoiGrid(parm);
		return json;
	}
	
	@RequestMapping(value = "/downloadSspoi")
	public void downloadSspoi(HttpServletRequest request, HttpServletResponse response) throws Exception{
		SspoiModel model = RequestUnit.getModel(SspoiModel.class);
		HashMap<String, Object> parm = new HashMap<String, Object>();
		if(model.getAddr() != null && !model.getAddr().equals("")){
			parm.put("addr", "%" + model.getAddr() + "%");
		}
		if(model.getPoiname() != null && !model.getPoiname().equals("")){
			parm.put("mc", "%" + model.getPoiname() + "%");
		}
		if(model.getTypeid() != null && !model.getTypeid().equals("")){
			parm.put("type", model.getTypeid());
		}
		if(model.getPcsid() != null && !model.getPcsid().equals("")){
			parm.put("pcs", model.getPcsid());
		}
		if(model.getJqid() != null && !model.getJqid().equals("")){
			parm.put("jq", model.getJqid());
		}
		if(model.getIsmarked() != null && !model.getIsmarked().equals("")){
			parm.put("ismark", model.getIsmarked());
		}
		log.info("==>addr="+model.getAddr()+",poiname="+model.getPoiname()+",typeid="+model.getTypeid()+",pscid="+model.getPcsid()+",jqid="+model.getJqid()+",ismarked="+model.getIsmarked());
		List<HashMap<String, Object>> list = service.download(parm);
		for(int i = 0; i < list.size(); i++){
			int ismark = ((BigDecimal)list.get(i).get("ISMARKED")).intValue();
			if(ismark == 0){
				list.get(i).put("ismark", "否");
			} else if(ismark == 1){
				list.get(i).put("ismark", "是");
			}
		}
		LinkedHashMap<String, String> column = new LinkedHashMap<String, String>();
		column.put("ID", "序号");
		column.put("CODENAME", "类型");
		column.put("PCSNAME", "派出所");
		column.put("TEL", "电话号码");
		column.put("JQNAME", "警区");
		column.put("POINAME", "信息点名称");
		column.put("ADDR", "地址");
		column.put("ismark", "是否标注");
		HSSFWorkbook wb = ExcelUtil.createExcel(list, column);
		OutputStream outStream = response.getOutputStream();
		response.setContentType("application/x-msdownload");  
		response.setHeader("Content-Disposition","attachment;filename=Sspoi.xls");  
		wb.write(outStream);
	}
	
	@RequestMapping(value = "/getModel")
	public ModelAndView getModel(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUnit.getString("id");
		HashMap<String, Object> map = service.getModel(id);
		request.setAttribute("model", map);
		return new ModelAndView("/view/manager/sspoi/markedPoi");
	}
	
	@RequestMapping(value = "/editModel")
	public @ResponseBody String editModel(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUnit.getString("id");
		HashMap<String, Object> map = service.getModel(id);
		String json = JsonUtil.ConvertObjectToJson(map);
		return json;
	}
	
	@RequestMapping(value = "/getModelJson")
	public @ResponseBody String getModelJson(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUnit.getString("id");
		HashMap<String, Object> map = service.getModel(id);
		String json = JsonUtil.ConvertObjectToJson(map);
		return JsonResultUtil.Success(json);
	}
	
	@RequestMapping(value = "/updateMarked")
	public @ResponseBody String updateMarked() throws MessageException{
		String id = RequestUnit.getString("id");
		String mapxy = RequestUnit.getString("mapxy");
		String addr = RequestUnit.getString("addr");
		String mapx = null, mapy = null, json = null;
		if(mapxy != null && !mapxy.equals("")){
			mapx = mapxy.split(",")[0];
			mapy = mapxy.split(",")[1];
		}
		SspoiModel model = new SspoiModel();
		try {
			if(id != null && !id.equals("")){
				model.setId(Double.valueOf(id));
				model.setAddr(addr);
				model.setIsmarked(Double.valueOf(1));
				model.setMapx(new BigDecimal(mapx));
				model.setMapy(new BigDecimal(mapy));
				model.setUpdateby(SessionUnit.getUserId());
				log.info("id="+id+",addr="+addr+",mapx="+mapx+",mapy="+mapy);
				service.update(model);
				json = "{\"code\":\"1\",\"msg\":\"标注成功\",\"success\":\"true\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new MessageException("标注失败", "3");
		}
		return json;
	}
	
	@RequestMapping(value = "/getCountList")
	public @ResponseBody String getCountList(){
		String json = service.getCountList();
		return json;
	}
	
	@RequestMapping(value = "/insert")
	public @ResponseBody String insert() throws MessageException{
		SspoiModel model = RequestUnit.getModel(SspoiModel.class);
		if((model.getMapx() != null && !model.getMapx().equals("")) && (model.getMapy() != null && !model.getMapy().equals(""))){
			model.setIsmarked(new Double(1));
		}else{
			model.setMapx(null);
			model.setMapy(null);
			model.setIsmarked(new Double(0));
		}
		model.setCreateby(SessionUnit.getUserId());
		String json = null;
		try {
			service.insert(model);
			json = "{\"code\":\"1\",\"msg\":\"新增成功\",\"success\":\"true\"}";
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e);
			throw new MessageException("新增失败", "3");
		}
		return json;
	}
	
	@RequestMapping(value = "/delete")
	public @ResponseBody String delete() throws MessageException{
		String ids = RequestUnit.getString("ids");
		log.info("警务信息点删除==》ids：" + ids);
		String json = null;
		try {
			service.delete(ids);
			json = "{\"code\":\"1\",\"msg\":\"删除成功\",\"success\":\"true\"}";
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e);
			throw new MessageException("删除失败", "3");
		}
		return json;
	}
	
	@RequestMapping(value = "/showMapCount")
	public @ResponseBody String showMapCount(){
		String typeid = RequestUnit.getString("typeid");
		List<HashMap<String, Object>> list = service.showMapCountByType(typeid);
		String json = JsonUtil.ConvertArrayToJson(list);
		return json;
	}
	
	@RequestMapping(value = "/getCenterCount")
	public @ResponseBody String getCenterCount(){
		List<HashMap<String, String>> list = service.getCenterCount();
		String json = JsonUtil.ConvertArrayToJson(list);
		return json;
	}
	
	@RequestMapping(value="/getPoiListMap")
	public @ResponseBody String getPoiListMap(@RequestParam(value="orgid",required=false) String orgid,
			@RequestParam(value="maxx",required=false) String maxx,
			@RequestParam(value="maxy",required=false) String maxy,
			@RequestParam(value="minx",required=false) String minx,
			@RequestParam(value="miny",required=false) String miny,
			@RequestParam(value="poiType",required=false) String poiType) throws Exception{
		HashMap<String, String> parm = new HashMap<String, String>();
		if(orgid != null && !orgid.equals("")){
			parm.put("orgid", orgid+"%");
		}
		
		parm.put("maxx", maxx);
		parm.put("maxy", maxy);
		parm.put("minx", minx);
		parm.put("miny", miny);
		parm.put("poiType", poiType);
		if(orgid != null && !orgid.equals("")){
			orgid = orgid + "%";
		}
		List<HashMap<String, String>> models = service.getPoiListMap(parm);
		
		String result = JsonUtil.ConvertArrayToJson(models);
		return result;
	}
}
